*********************************************************************************
/*  The Marginal Value of Public Funds of Marginal Tax Audits (high vs. low-income)	*/
*********************************************************************************
* Set directories 
* File paths
local datapath "${mvpf_audits}/data/cleaned" 
local datafolder "${mvpf_audits}/data/outside" 
local graphpath "${graphpath}" 

* Version of fig to produce 
global version  ${version} // paper or slides

* Other formatting
if "${version}" == "slides" {
	global title "title(" ", size(vhuge))"
	global img wmf
	graph set window fontface "Arial"
	global title "title(" ", size(vhuge))"
	global axis_size "" //xsize(6.5) ysize(4)" // makes figure wider
}

else if "${version}" == "paper" {
	global title "title("")"
	global img pdf
	graph set window fontface "LMRoman10-Regular"
	global title " "
	global axis_size " " // makes figure wider
}

/*------------------------------------------------------------------------------
 Figure: MVPF construction (20-30 pctl TPI)
------------------------------------------------------------------------------*/
use "${mvpf_audits}/data/figures_rep/figure_8.dta", clear

forv d = 20(70)90{
	local d2 = `d' + 10
preserve
* Keep TPI decile of interest
	keep if decile==`d' 
	
	* Save marginal R/C
	qui summ rc_marginal 
	local rc = r(mean)
	
	* Save deterrence effect
	qui summ determult99_dis_const
	local rev_determult = r(mean)
	
	qui summ hassle
	local hc = r(mean)
	
	local rc2 = `rc' // revenue/cost
	local ec2 = `rev_determult'*`rc' // case where E = R
	local hc2 = `hc' // hassle cost
	
	local mvpf: di %4.2f  (`rc2' + `ec2' + `hc2')/(-1 + `rc2' + `ec2')
 
	* Manual inputs
	drop _all
	set obs 100
	g xaxis = _n
	replace xaxis = 0.99 in 1

	local max_obs = 9 // Total number of components + 1

	local white_out_first_cost = 2 // Observation first white out bar appears (always true)
	local white_out_last_cost = 3 // Observation last white out bar appears
	local white_out_first_wtp = 7 // Observation first white out bar appears (always true) - cost
	local white_out_last_wtp = 8 // Observation last white out bar appears - cost

	local xaxis_total_cost = 4
	local xaxis_total_wtp = 9

	*** Cost Data Inputs
	gen cost =  -1 in 1 // upfront spending
	replace cost =  `rc2' in 2 // R/C
	replace cost = `ec2' in 3 // Deterrence effect
	replace cost = -1 + `rc2' + `ec2' in 4 // Total WTP 
	replace cost = . in 5 //
	
	g cost2 = cost
	g white_out = cost2
	
	replace white_out = cost2 if inrange(xaxis, 0, `xaxis_total_cost')
	
	forval i = `white_out_first_cost'/`white_out_last_cost' {
		replace white_out = cost2[`i'-1]  in `i' if cost>0
		replace cost2 = cost2[`i'-1] + cost  in `i' if cost>0
	
		replace white_out = cost2[`i'- 1]  + cost in `i' if cost<0 & cost[`i'-1]>0 
		replace cost2 = cost2[`i'-1] in `i' if cost<0 & cost[`i'-1]>0 
	
		replace white_out = white_out[`i'- 1]  + cost in `i' if cost<0 & cost[`i'-1]<0 
		replace cost2 = white_out[`i'-1] in `i' if cost<0 & cost[`i'-1]<0 	
	
		replace white_out = cost[`i'- 1]  in `i' if cost>0 & cost[`i'-1]<0 
	}
	

	g cost3=sum(cost) // to see if cost[i] + cost[i-1] is > or < 0

	g grey_bar = .
	replace grey_bar = white_out if xaxis== 2
	

	*** WTP Data Inputs 
	gen wtp = `rc2'  in 6 // upfront revenue
		replace wtp = `ec2' in 7 // deterrence
		replace wtp = `hc2' in 8 // hassle cost
		replace wtp = `rc2' + `ec2' + `hc2' in 9
	
	g wtp2= wtp
	replace white_out = wtp2 if inrange(xaxis, 6, `xaxis_total_wtp')
	
	forv i = `white_out_first_wtp'/`white_out_last_wtp' {
		replace white_out = wtp2[`i'-1]  in `i' if wtp>0
		replace wtp2 = wtp2[`i'-1] + wtp  in `i' if wtp>0
	
		replace white_out = wtp2[`i'- 1]  + wtp in `i' if wtp<0 & wtp[`i'-1]>0 
		replace wtp2 = wtp2[`i'-1] in `i' if wtp<0 & wtp[`i'-1]>0 
	
		replace white_out = white_out[`i'- 1]  + wtp in `i' if wtp<0 & wtp[`i'-1]<0 
		replace wtp2 = white_out[`i'-1] in `i' if wtp<0 & wtp[`i'-1]<0 
	}
	
	*** set y-axis label and figure name
	local ylab = ""
	local fig_name = "waterfall_mvpf_`d'_`d2'"

	drop if xaxis>`max_obs'

	*** set fig range (y-axis)
	if `d'==20 {
		local lb = -2
		local ub = 6
		local int = 2	
		local lb_f : di %02.1f `lb'
		local ub_f : di %02.1f `ub'
	}
	
	if `d'==90 {
		local lb = -3
		local ub = 15
		local int = 3	
		local lb_f : di %02.1f `lb'
		local ub_f : di %02.1f `ub'
	}
	
	if "${version}" == "paper" {
	    global ylab "ylabel(`lb_f'(`int')`ub_f', nogrid )"
	}
	
	if "${version}" == "slides" & `d'==90 {
	    global ylab "ylabel(-5 "-5" 0 "0" 5 "5" 10 "10" 15 "15", nogrid )"
	}
	
	if "${version}" == "slides" & `d'==20 {
	    global ylab "ylabel(-2 "-2" 0 "0" 2 "2" 4 "4" 6 "06", nogrid )"
	}

	*** set y-coordinate for text/arrow
	local y = -1

	*** Labels 
	g label_wtp = "$"+ string(wtp, "%4.2f")  
	
	g label_cost = "$"+ string(cost, "%4.2f")  
		replace label_cost = "-" +"$"+ string(abs(cost), "%4.2f") if cost<0
			
	*** Generate chart 
	tw  (bar wtp xaxis if xaxis==`xaxis_total_wtp', barwidth(0.6)  lcolor(%0) color("0 114 178"%80))  ///
		(bar wtp2 xaxis if wtp>=0 & xaxis!=`xaxis_total_wtp' , barwidth(0.6)  lcolor(white%0) color(gs11%80)) ///
		(bar wtp2 xaxis if wtp<0 , barwidth(0.6)  lcolor(white%0) color(gs11%80)) ///
		(bar cost xaxis if xaxis==`xaxis_total_cost', barwidth(0.6)  lcolor(%0) color(cranberry))  ///
		(bar cost2 xaxis if cost>=0 & xaxis!=`xaxis_total_cost' , barwidth(0.6) lcolor(white%0) color(gs11%80)) ///
		(bar cost2 xaxis if cost<0 & xaxis!=`xaxis_total_cost', barwidth(0.6)  lcolor(white%0) color(gs11%80)) ///
		(bar white_out xaxis if inrange(xaxis,`white_out_first_wtp',`white_out_last_wtp') | ///
			inrange(xaxis,`white_out_first_cost',`white_out_last_cost') , ///
			color(white) lcolor(white) lw(thin) barwidth(0.65)) ///
		(bar grey_bar xaxis  , barwidth(0.6)  lcolor(white%0) color(gs11%80)) ///
		(scatter wtp2 xaxis if wtp>0, mlab(label_wtp) mlabs(small) msym(none) mlabc("$tc") mlabpos(12)) ///
		(scatter white_out xaxis if wtp<0, mlab(label_wtp) mlabs(small) msym(none) mlabc("$tc") mlabpos(6)) ///
		(scatter cost2 xaxis if cost<0 , mlab(label_cost) mlabs(small) msym(none) mlabc("$tc") mlabpos(6)) ///
		(scatter cost2 xaxis if cost>0 & ~missing(cost) , mlab(label_cost) mlabs(small) msym(none) mlabc("$tc") mlabpos(12)) ///
		///(pcarrowi `y' 6.0 `y' 6.3, color(black) ) ///
		(function y =0, lcolor(gs11) lwidth(thin) lp(solid) range(0.3 `=9.7')) ///
		, ///
		text(`y' 7.1 "MVPF = `mvpf'", size(3.5))  ///
		${ylab} yscale(r(`lb_f' `ub_f')) ///
		legend(off) ytitle("`ylab'") xtitle("")   ///
		${title} ${axis_size} ///
		xlabel( ///
		1 `""Initial" "Spending""' ///
		2 `""Audit" "Revenue""' ///
		3 `""Deterrence" "Revenue""' ///
		4 `""{bf:Net}" "{bf:Revenue}""' ///
		6 `""Audit" "Revenue""' ///
		7 `""Deterrence" "Revenue""' ///
		8 `""Taxpayer" "Burden""' ///
		9 `""{bf:Net}" "{bf:WTP}""' ///	
		, labsize(small) notick nogrid )  xscale(r(0.2 9.8))
		graph export "`graphpath'/`fig_name'.wmf", as(wmf)  replace
		
		restore
}
